package com.example.shop.service.impl;

import com.example.common.common.config.filter.UserContext;
import com.example.common.utils.Assert;
import com.example.common.utils.MinioUtils;
import com.example.shop.entity.Cart;
import com.example.shop.entity.GoodsSku;
import com.example.shop.entity.Shop;
import com.example.shop.mapper.CartMapper;
import com.example.shop.mapper.GoodsSkuMapper;
import com.example.shop.mapper.ShopMapper;
import com.example.shop.model.req.CartDTO;
import com.example.shop.model.req.CartSkuDTO;
import com.example.shop.model.resp.CartDetail;
import com.example.shop.model.resp.GoodsDetail;
import com.example.shop.service.CartService;
import com.example.shop.service.GoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CartServiceImpl implements CartService {
    private final GoodsService goodsService;
    private final GoodsSkuMapper goodsSkuMapper;
    private final CartMapper cartMapper;
    private final MinioUtils minioUtils;
    private final ShopMapper shopMapper;

    @Override
    public void addOrEditCart(CartDTO cartDTO) {
        // 查询用户的购物车是否存在商品
        String userId = UserContext.getCurrentUser().getUserId();
        Cart cart = cartMapper.queryUserCartByParm(userId, cartDTO.getGoodsId(), cartDTO.getSkuKey());
        if (cart != null) {
            // 商品已存在，更新数量
            cart.setNums(cart.getNums() + cartDTO.getQuantity());
            cartMapper.updateCart(cart);
        } else {
            // todo 校验店铺id
            GoodsDetail goodsDetail = goodsService.getGoodsDetail(cartDTO.getGoodsId());
            GoodsSku goodsSku = goodsSkuMapper.queryGoodsSkuByGoodsIdWithSkuId(cartDTO.getGoodsId(), cartDTO.getSkuKey());

            Assert.notNull(goodsDetail, "商品不存在");
            Assert.notNull(goodsSku, "商品选项不存在");
            cart = Cart.builder()
                    .userId(userId)
                    .goodsId(cartDTO.getGoodsId())
                    .shopId(goodsDetail.getShop().getShopCode())
                    .nums(cartDTO.getQuantity())
                    .skuId(cartDTO.getSkuKey())
                    .build();

            // todo sku的库存足够才能加入购物车

            cartMapper.addCart(cart);
        }

    }

    @Override
    public List<CartDetail> listCart() {
        String userId = UserContext.getCurrentUser().getUserId();

        List<CartDetail> cartDetails = cartMapper.queryUserCartDetail(userId);
        List<Shop> shopList = shopMapper.queryShopList();
        Map<String, Shop> shopMap = shopList.stream().collect(Collectors.toMap(Shop::getShopId, Function.identity()));
        for (CartDetail cartDetail : cartDetails) {
            cartDetail.setImage(minioUtils.getObjectUrl(cartDetail.getImage()));
            cartDetail.setShopName(shopMap.get(cartDetail.getShopCode()).getShopName());
        }
        return cartDetails;
//        List<CartDetail> cartDetails = new ArrayList<>();
//        for (Cart cart : carts) {
//            CartDetail cartDetail = CartDetail.builder()
//                    .goodsId(cart.getGoodsId())
//                    .skuKey(cart.getSkuId())
//                    .quantity(cart.getNums())
//                    .title()
//                    .build();
//        }
//        return List.of();
    }

    @Override
    public void deleteCart(CartSkuDTO cartSkuDTO) {
        String userId = UserContext.getCurrentUser().getUserId();
        cartMapper.deleteCart(userId, cartSkuDTO.getGoodsId(), cartSkuDTO.getSkuKey());
    }
}
